package servlet;
import bean.Login;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
@WebServlet(name = "HandleLogin", value = "/HandleLogin")
public class HandleLogin extends HttpServlet {
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
    }
    public void service(HttpServletRequest request,
                        HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection con=null;
        String userID=request.getParameter("loginID").trim();
        String password=request.getParameter("password").trim();
        System.out.println(password);
        try {
            password=Md5Utils.encoder(password);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        System.out.println(password);
        try{
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system","root","123456");
            Statement stmt=conn.createStatement();
            String sql="select* from users where UserID='"+userID+"' and Password='"+password+"'";
                ResultSet rs= stmt.executeQuery(sql);
                boolean m=rs.next();
                if (m){
                  success(request,response,userID,password);
                  RequestDispatcher dispatcher=request.getRequestDispatcher("index.jsp");
                  dispatcher.forward(request,response);
                }else{
                  String backNews="您输入的用户名不存在，或密码不匹配";
                  System.out.println(backNews);
                  fail(request,response,userID,backNews);
            }
        con.close();
    }catch(SQLException | ClassNotFoundException exp){
        String backNews="";
        fail(request,response,userID,backNews);
    }finally{
        try{
            con.close();
        }catch (Exception ee){}
    }}

    public void success(HttpServletRequest request,HttpServletResponse response,String userID,String password){
      Login loginBean=null;
      HttpSession session=request.getSession(true);
      try{loginBean=(Login)session.getAttribute("loginBean");
      if(loginBean==null){
          loginBean=new Login();
          session.setAttribute("loginBean",loginBean);
    }
       String name=loginBean.getLoginID();
       if(name.equals(userID)){
           loginBean.setBackNews(userID+"已经登陆了");
           loginBean.setLoginID(userID);//输入姓名
           System.out.println("成功写入JAVAbean");
    }else{
        loginBean.setBackNews(userID+"登陆成功");
        loginBean.setLoginID(userID);
    }
    }catch (Exception ee){
        loginBean=new Login();
        session.setAttribute("loginBean",loginBean);
        loginBean.setBackNews(ee.toString());
        System.out.println("ee"+ee);
        loginBean.setLoginID(userID);
    }}
    public void fail(HttpServletRequest request,HttpServletResponse response,String userID,String backNews){
    response.setContentType("text/html;charset=utf-8");
    try{
        PrintWriter out=response.getWriter();
        out.println("<html><body>");
        out.println("<h2>用户"+userID+"登陆反馈结果<br>"+backNews+"</h2>");
        System.out.println("12"+backNews);
        out.println("<a href=login.jsp>登录页面</a>");
        out.println("<a href=index.jsp>主页</a>");
        out.println("</body></html>");
    }catch(IOException exp){}
    }
}
